<?php
	/*
	 * 引用一个类的属性和方法就使用 -> 符号	
	 * -> 这个箭头也可以是调用类中的函数
	 */
	header("Content-Type: text/html;charset=utf-8");

	/*$servername = "localhost";
	$username = "root";
	$password = "456789";
	$dbname = "mysqlstudy";*/

	/*创建连接
	$conn = new mysqli($servername, $username, $password, $dbname);
	检测连接
	if($conn -> connect_error){
		die("连接失败：" . $conn -> connect_error);
	}
	设置编码，防止中文乱码
	mysqli_query($conn, "set names utf8");
	$count = "SELECT COUNT(*) from tp_testorder";*/
	
	/*
	 *分页的函数
	 *pageNum 为页数，pageSize为一个页面显示几条数据
	 */
	function news($pageNum = 1, $pageSize = 10){
		$array = array();
		
		$conn = new mysqli("localhost", "root", "456789", "mysqlstudy");
		if($conn -> connect_error){
			die("连接失败：" . $conn -> connect_error);
		}
		mysqli_query($conn, "set names utf8");

		//limit为约束显示多少条信息，后面两个参数，第一个为从第几个开始，第二个为长度
		$rs = "SELECT * from tp_testorder where id<300 limit " . (($pageNum-1)*$pageSize) . "," . $pageSize;
		$r = $conn -> query($rs);
		//mysql_fetch_object() 函数从结果集（记录集）中取得一行作为对象
		while($obj = $r -> fetch_object()){
			$array[] = $obj;
		}

		$conn->close();
		return $array;
	}

	/*
	 *显示总行数的函数
	 */
	function allNews(){
		$conn = new mysqli("localhost", "root", "456789", "mysqlstudy");
		if($conn -> connect_error){
			die("连接失败：" . $conn -> connect_error);
		}
		mysqli_query($conn, "set names utf8");


		$rs = "SELECT COUNT(*) num from tp_testorder WHERE id<300"; //显示总行数
		$r = $conn -> query($rs);
		$obj = $r -> fetch_object();
		$conn -> close();
		return $obj -> num;
	}
  
	$allNum = allNews();  //总页数

	$pageSize = 10;  //预定每页显示的信息条数
	$pageNum = empty($_GET["pageNum"]) ? 1 : $_GET["pageNum"];   
	//empty:检查一个变量是否为空值 
	$endPage = ceil($allNum/$pageSize);  //总页数 ceil:向上舍入为最接近的整数
	$array = news($pageNum,$pageSize);

?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>PHP分页STUDY</title>
  <style>
  	table{
  		background-color:#fff;color:#666;border-collapse: collapse;border-spacing: 0;
  	}
  	table td{
  		padding:5px 0;line-height:20px;font-size:14px;border-width:1px;border-style:solid;
  		border-color:#e6e6e6;box-sizing:border-box;border-top:none;border-left:none;
  		overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:300px;
  	}
  </style>
</head>
<body>
	<table cellpadding="0" cellspacing="0" border="0">
		<tr>
			<th>ID</th><th>OID</th><th>TYPE</th><th>TEL</th><th>NAME</th>
			<th>CONTENT</th><th>JSON</th><th>CDATE</th><th>UPDATE</th>
		</tr>
		<?php 
			foreach ($array as $key => $value){
				echo "<tr>";
				echo "<td>{$value->id}</td>";
				echo "<td>{$value->oid}</td>";
				echo "<td>{$value->type}</td>";
				echo "<td>{$value->tel}</td>";
				echo "<td>{$value->name}</td>";
				echo "<td>{$value->content}</td>";
				echo "<td>{$value->json}</td>";
				echo "<td>{$value->cdate}</td>";
				echo "<td>{$value->udate}</td>";
				echo "</tr>";
			}

		?>
	</table>
	<div>
		<a href="?pageNum=1">首页</a>
		<a href="?pageNum=<?php echo $pageNum==1?1:($pageNum-1) ?>">上一页</a>
		<a href="?pageNum=<?php echo $pageNum==$endPage?$endPage:($pageNum+1) ?>">下一页</a>
		<a href="?pageNum=<?php echo $endPage?>">尾页</a>
		<span>共<?php echo $endPage ?>页</span>
		<span>第<?php echo $pageNum ?>页</span>
	</div>
	
</body>
</html>


